package com.ba.base.view.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity

abstract class BaseDialog : Dialog {

    constructor(context: Context) : super(context)

    constructor(context: Context, themeResId: Int) : super(context, themeResId)

    constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener)
            : super(context, cancelable, cancelListener)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getContentViewResId() != 0) {
            setContentView(getContentViewResId())
            onSetContentViewAfter()
            onInitViewBefore()
            initView()
            onInitViewAfter()
        }
    }

    protected open fun onSetContentViewAfter() {
        window?.run {
            setGravity(Gravity.CENTER)
            val windowManager = windowManager
            val display = windowManager.defaultDisplay
            val lp = attributes
            val displayMetrics = DisplayMetrics()
            display.getMetrics(displayMetrics)
            lp.width = (displayMetrics.widthPixels * 0.85).toInt()
            attributes = lp
        }
    }

    protected open fun onInitViewBefore() {}

    protected open fun onInitViewAfter() {}

    protected abstract fun getContentViewResId(): Int

    protected abstract fun initView()
}